package com.chuanyun.wechatcheckin.controller;

import com.chuanyun.wechatcheckin.service.BalanceLeaveProcessService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 调休申请处理控制器
 * 提供调休申请数据处理的API接口
 */
@Slf4j
@RestController
@RequestMapping("/api/balance-leave")
@RequiredArgsConstructor
public class BalanceLeaveController {

    private final BalanceLeaveProcessService balanceLeaveProcessService;

    /**
     * 处理调休申请数据
     * 自动查询氚云调休申请记录，并更新企业微信假期余额
     * 
     * @return 处理结果
     */
    @PostMapping("/process")
    public ResponseEntity<Map<String, Object>> processBalanceLeaveApplications() {
        log.info("收到调休申请处理请求");
        
        Map<String, Object> response = new HashMap<>();
        
        try {
            // 调用服务处理调休申请
            balanceLeaveProcessService.processBalanceLeaveApplications();
            
            // 处理成功
            response.put("success", true);
            response.put("message", "调休申请处理完成");
            response.put("timestamp", System.currentTimeMillis());
            
            log.info("调休申请处理成功");
            return ResponseEntity.ok(response);
            
        } catch (Exception e) {
            log.error("调休申请处理失败", e);
            
            // 处理失败
            response.put("success", false);
            response.put("message", "调休申请处理失败: " + e.getMessage());
            response.put("error", e.getClass().getSimpleName());
            response.put("timestamp", System.currentTimeMillis());
            
            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 获取调休申请处理状态
     * 
     * @return 服务状态信息
     */
    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getProcessStatus() {
        log.info("收到调休申请处理状态查询请求");
        
        Map<String, Object> response = new HashMap<>();
        
        try {
            response.put("success", true);
            response.put("message", "调休申请处理服务运行正常");
            response.put("service", "BalanceLeaveProcessService");
            response.put("timestamp", System.currentTimeMillis());
            
            // 可以添加更多状态信息
            Map<String, Object> config = new HashMap<>();
            config.put("overtimeTableSchemaCode", "D290920sjxsawd7uuw5tupspi71");
            config.put("balanceLeaveType", "Balance Leave 申请调休");
            config.put("balanceLeaveName", "Balance Leave  调休假");
            config.put("vacationId", 4);
            
            response.put("config", config);
            
            return ResponseEntity.ok(response);
            
        } catch (Exception e) {
            log.error("获取调休申请处理状态失败", e);
            
            response.put("success", false);
            response.put("message", "获取状态失败: " + e.getMessage());
            response.put("error", e.getClass().getSimpleName());
            response.put("timestamp", System.currentTimeMillis());
            
            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 健康检查接口
     * 
     * @return 健康状态
     */
    @GetMapping("/health")
    public ResponseEntity<Map<String, Object>> healthCheck() {
        Map<String, Object> response = new HashMap<>();
        response.put("status", "UP");
        response.put("service", "BalanceLeaveController");
        response.put("timestamp", System.currentTimeMillis());
        
        return ResponseEntity.ok(response);
    }
}